package com.boot.learning.async;

import java.util.Random;

import org.springframework.stereotype.Component;

/**
 * 同步业务 （任务依次执行）
 * 
 * @version 1.0
 * @author fuhw
 */
@Component
public class SyncMethodDemo {
	
	public static Random random = new Random();
	
	public void executeTask() throws Exception {
		
		doTaskOne();
		
		doTaskTow();
		
		doTaskThree();
		
		System.err.println( "任务完成！" );
	}

	private void doTaskThree() throws InterruptedException{
		System.out.println( "开始做任务三：" );
		long startTow = System.currentTimeMillis();
		Thread.sleep( random.nextInt( 10000 ) );
		long endTow = System.currentTimeMillis();
		System.out.println( "任务三执行完成，耗时：" + (endTow - startTow) + "毫秒" );
	}

	private void doTaskTow() throws InterruptedException{
		System.out.println( "开始做任务二：" );
		long startTow = System.currentTimeMillis();
		Thread.sleep( random.nextInt( 10000 ) );
		long endTow = System.currentTimeMillis();
		System.out.println( "任务二执行完成，耗时：" + (endTow - startTow) + "毫秒" );
	}

	private void doTaskOne() throws InterruptedException{
		System.out.println( "开始做任务一：" );
		long startOne = System.currentTimeMillis();
		Thread.sleep( random.nextInt( 10000 ) );
		long endOne = System.currentTimeMillis();
		System.out.println( "任务一执行完成，耗时：" + (endOne - startOne) + "毫秒" );
	}
}
